local extension = Package:new("nanxiao")
extension.extensionName = "Euphonium"
local U = require "packages/utility/utility"
Fk:loadTranslationTable{
    ["nanxiao"] = "南中四小",
}

--义井沙里
local yijingshali = General:new(extension,"yijingshali","th_k_chui",3,3,General.Female)
local yigongqing = fk.CreateTriggerSkill{
  name = "yigongqing",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and not target.dead and player:inMyAttackRange(target)
   end,
   on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    room:notifySkillInvoked(player, self.name, event == fk.Damaged and "masochism" or "drawcard")
    room:drawCards(player, 2, self.name)
    local to = event == fk.Damaged and data.to or data.from
    if player.dead or player:isNude() or to.dead then return false end
    local card = room:askForCard(player, 1, 1, true, self.name, false, ".", "#yi_gongqing-give::"..to.id)
    if #card > 0 then
      room:moveCardTo(card, Card.PlayerHand, to, fk.ReasonGive, self.name, nil, false, player.id)
    end
  end
}
local yigongqing_daged = fk.CreateTriggerSkill{
  name = "#yigongqing_daged",
  events = {fk.EventPhaseStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player:getHandcardNum() > player.hp and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    room:damage{
      to = player,
      damage = 1,
      skillName = self.name,
    }
  end
}

local eup_fuwei = fk.CreateTriggerSkill{
  name = "eup_fuwei",
  events = {fk.EventPhaseStart},
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(room:getOtherPlayers(player, false), Util.IdMapper)
    local targetRecorded = {}
    for _ = 1, player.hp, 1 do
      if player.dead or player:isNude() then break end
      local tos, cardId = room:askForChooseCardAndPlayers(
        player,
        targets,
        1,
        1,
        ".",
        "#fuwei",
        self.name,
        true,
        true
      )
      if #tos > 0 then
        local to = room:getPlayerById(tos[1])
        room:obtainCard(to, cardId, false, fk.ReasonGive)
        room:addPlayerMark(to, "@@fuwei")
        table.insert(targetRecorded, tos[1])
        targets = table.filter(targets, function (pid)
          return tos[1] ~= pid and not room:getPlayerById(pid).dead
        end)
        if #targets < 1 then break end
      else
        break
      end
    end

    player:setMark("fuwei_targets", targetRecorded)
  end,

  refresh_events = {fk.TurnStart, fk.Death},
  
  can_refresh = function(self, event, target, player, data)
    return target == player and type(player:getMark("fuwei_targets")) == "table"
  end,

  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local targets = player:getMark("fuwei_targets") or {}

    for _, pid in ipairs(targets) do
      room:removePlayerMark(room:getPlayerById(pid), "@@fuwei",100)
    end

    player:setMark("fuwei_targets", {})
  end,
}
local fuwei_attackrange = fk.CreateAttackRangeSkill{
  name = "#fuwei_attackrange",
  main_skill = eup_fuwei,
  within_func = function (self, from, to)
    if from:hasSkill("fuwei") then
      return to:getMark("@@fuwei") > 0
    end
  end,
}
local gongxia = fk.CreateTriggerSkill{
  name = "gongxia",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and not player.dead
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use = room:askForUseCard(player, "slash", "slash", "#gongxia-choose", true, {bypass_times = true})
      if use then
        room:useCard(use)
        if use.damageDealt then
          local recover_targets = table.map(room.alive_players, Util.IdMapper)
          local recover_to = room:askForChoosePlayers(player, recover_targets, 1, 1, "#gongxia-recover", self.name, true)
          if #recover_to > 0 then
            room:recover({
              who = room:getPlayerById(recover_to[1]),
              num = 1,
              recoverBy = player,
              skillName = self.name
            })
          end
          fuwei:use(event, target, player, data)
        end
      end
    end
}

yigongqing:addRelatedSkill(yigongqing_daged)
yijingshali:addSkill(yigongqing)
eup_fuwei:addRelatedSkill(fuwei_attackrange)
yijingshali:addSkill(eup_fuwei)
yijingshali:addSkill(gongxia)
Fk:loadTranslationTable{
  ["yijingshali"] = "义井沙里",
  ["#yijingshali"] = "凌强扶弱",
  ["yigongqing"] = "共情",
  [":yigongqing"] = "当你攻击范围内一名其他角色受到伤害后，你可以摸两张牌然后交给其一张牌。准备阶段，若你的手牌数大于你的体力值，你受到一点伤害。",
  ["#yi_gongqing-give"] = "共情：选择一张牌，交给 %dest",
  ["#yigongqing_daged"] = "共情",
  ["eup_fuwei"] = "抚慰",
  [":eup_fuwei"] = "出牌阶段开始时，你可以交给至多X名角色各一张牌，若如此做，获得牌的角色视为在你的攻击范围内至你下个回合开始。（X为你的体力值）",
  ["#fuwei"] = "是否发动抚慰：选择一名角色交给其一张牌",
  ["@@fuwei"] = "抚慰",
  ["gongxia"] = "攻瑕",
  [":gongxia"] = "当你受到一点伤害时，你可以对一名其他角色使用一张杀,若此杀造成伤害，则你可以令一名角色回复一点体力，且你可以发动一次“抚慰”",
  ["#gongxia-choose"] = "选择一名角色对其使用一张杀",
  ["#gongxia-recover"] = "选择一名角色令其回复一点体力"

}
return extension